//index.js
//获取应用实例
const app = getApp()
import { affirmOrder,addorder,queryAddrList,topay } from '../../http/api.js';

Page({
  data: {
    businessId: 0,
    addressId: 0,
    addrInfo:{},
    addrList:[],
    dataInfo:{},
    showAddr: false,
    orderId:0
  },
  onLoad: function (options) {
    this.setData({
      businessId: options.businessId
    });
  },
  onShow: function(){
    this.getAffirmOrder();
    this.queryAddrList();
  },
  //隐藏收件人弹窗
  disPopu: function(){
    this.setData({
      showAddr: false
    })
  },
  //展示收件人弹窗
  showPopu: function(){
    this.setData({
      showAddr: true
    })
  },
  //切换收货地址
  tabOrderAddr: function(e){
     let addrInfo = e.currentTarget.dataset.item;
     this.setData({addrInfo});
     this.disPopu();
  },
  //添加收货地址
  goAddAttr: function(){
    wx.navigateTo({
      url: '/pages/address/add/index',
    })
  },
  //编辑收货地址
  editAddress: function(e){
    let addrVo = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: `/pages/address/edit/index?addrVo=`+JSON.stringify(addrVo)
    })
  },
  // 确认订单
  getAffirmOrder: function () {
    affirmOrder(this.data.businessId).then(res => {
      console.log(res);
      if (res.code == '200') {
        let dataInfo = res.data;
        this.setData({dataInfo});
      }
    })
  },
  //创建订单
  createOrder: function(){
    if(this.data.orderId){
      this.toWxPay();
    }else{
      let addressId = this.data.addrInfo.addressId;
      if(!addressId){
        wx.showToast({
          title:'请选择您的配送地址',
          duration: 2000,
          icon: 'none'
        })
        return;
      }
      let obj = {
        businessId:this.data.businessId,
        paymentType:'1',
        addressId:this.data.addrInfo.addressId,
        buyerMessage:'暂无备注'
      }
      let vm = this;
      addorder(obj).then(res=>{
        if (res.code == '200') {
          let orderId = res.data;
          vm.setData({orderId});
          vm.toWxPay();
        }
      });
    }
  },
  //获取支付参数
  toWxPay: function(){
    let orderId = this.data.orderId;
  
    topay(orderId).then(res=>{
      if (res.code == '200') {
        let json = res.data;
        wx.requestPayment({
          timeStamp: json.timeStamp,
          nonceStr: json.nonceStr,
          package: json.package,
          signType: json.signType,
          paySign: json.sign,
          success (res) {
            wx.switchTab({
              url: '/pages/order/index',
            });
          },
          fail (res) {
            
          }
        });
      }
    });
  },
  //查询收件人信息
  queryAddrList: function(){
    let obj = {	"current": 1, "size": 999};
    queryAddrList(obj).then(res=>{
     let addrList = res.rows;
     addrList.map(item=>{
      item['addrDesc'] = `${item.provinceName}${item.cityName}${item.districtName}${item.detailAddress}`;
      return item;
     });
     if(addrList.length  > 0){
      let addrInfo = addrList[0];
      this.setData({
        addrInfo,
        addrList
      });
     }
    });
  }
})
